Ventilator withdrawal algorithm